package com.wwt.server.exception;

import com.wwt.server.vo.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLTimeoutException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(SQLException.class)
    public Result mySqlException(SQLException e){
        if (e instanceof SQLIntegrityConstraintViolationException)
            return Result.fail("数据被关联，无法删除！",500);
        else if (e instanceof SQLTimeoutException)
            return Result.fail("SQL超时！",500);
        else if (e instanceof SQLException)
            return Result.fail("数据库相关异常!",500);
        else return Result.fail("服务器发生了错误！");
    }
}
